package com.example.demo.services.impl;
import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.BoardMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Board;
import com.example.demo.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

@Resource
private BoardMapper boardMapper;
@Override
public List<Board> selectByNum(Integer num) {
    // 非空校验
    if(num == null){
        // 日志记录
        log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        //抛出异常
        throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
    }
        //调用DAO 查询数据
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public Board selectById(Long id) {
        if(id==null || id<=0){
            // 日志记录
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board= boardMapper.selectByPrimaryKey(id);

        return board;
    }


    @Override
    public void addOneArticleCount(Long id) {
        // 非空校验
        if(id==null || id<=0){
            // 日志记录
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询板块信息
        Board board= boardMapper.selectByPrimaryKey(id);
        //判断信息是否为空
        if(board==null){
            // 日志记录
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        boardMapper.updateByPrimaryKeySelective(updateBoard);
}

    @Override
    public void subOneArticleCountById(Long id) {
        //非空校验
        if(id==null || id<=0){
            // 日志记录
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //查询板块信息
        Board board =boardMapper.selectByPrimaryKey(id);
        //判断信息是否为空
        if(board == null){
            // 日志记录
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        //构建新的板块信息
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }


        int row= boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1){
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }


    }
}
